﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Bootstrap5.Design;

internal abstract class BreakPointClassDecoratorProvider<TKey, TDefinitionModel,TDecorator>:ClassDecoratorProvider,IBreakPointDefinition<TDecorator>
    where TDefinitionModel : BreakingPointModel,new()
    where TDecorator : IBreakPointDefinition<TDecorator>
{
    protected Dictionary<TKey, List<TDefinitionModel>> RuleDic { get;  } = new();
    protected TDefinitionModel CurrentModel { get; private set; } = new();

    public TDecorator OnXS => GetBreakPoint(BreakPoint.XS);

    public TDecorator OnSM => GetBreakPoint(BreakPoint.SM);

    public TDecorator OnMD => GetBreakPoint(BreakPoint.MD);

    public TDecorator OnLG => GetBreakPoint(BreakPoint.LG);

    public TDecorator OnXL => GetBreakPoint(BreakPoint.XL);

    public TDecorator OnXXL => GetBreakPoint(BreakPoint.XXL);

    protected abstract TDecorator GetBreakPoint(BreakPoint breakPoint);
    protected abstract TDecorator GetKey(TKey key);

    protected void SetBreakPoint(BreakPoint breakPoint)
    {
        CurrentModel.BreakPoint = breakPoint;
        SetDirty();
    }

    protected void SetKey(TKey key)
    {
        CurrentModel = new();

        if (!RuleDic.ContainsKey(key))
        {
            RuleDic[key] = new List<TDefinitionModel> { CurrentModel };
        }
        else
        {
            RuleDic[key].Add(CurrentModel);
        }

        SetDirty();
    }

    protected string GetBreakPointString(BreakPoint? breakpoint)
    {
        if (breakpoint.HasValue)
        {
            return $"{breakpoint.Value.GetEnumCssClass()}-";
        }
        return string.Empty;
    }

    protected bool TryGetBreakPointString(BreakPoint? breakpoint,out string value)
    {
        value = GetBreakPointString(breakpoint);
        return !string.IsNullOrEmpty(value);
    }
}
